//
//  HWpinglunViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/11/11.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWpinglunViewController.h"

#import "HWCommentsTableViewCell.h"
#import "HWyouxipinglunModel.h"

@interface HWpinglunViewController ()<UITableViewDelegate,UITableViewDataSource,HWCommentsTableViewCellDelegate>

@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *gameArr;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSString *p;




@end

@implementation HWpinglunViewController



-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        _mainaTableVie.backgroundColor = RGB(247, 247, 247);
        _mainaTableVie.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"" titleStr:@"" detailStr:@"暂无数据"];
        _mainaTableVie.ly_emptyView.contentViewOffset = -40;
    }
    return   _mainaTableVie ;
}






- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {

    [super viewDidLoad];
    // yishiyongde   baisebeijingjilu

     [self setupUI];
    //  [self navigaidaohang];
    
    
    
}


- (void)navigaidaohang  {
    
    
    UIBarButtonItem *but =   [[UIBarButtonItem alloc]initWithTitle:@"标记为已读" style:UIBarButtonItemStylePlain target:self action:@selector(shiyongjilu)];
 
    but.tintColor = RGB(68, 68, 68);
    self.navigationItem.rightBarButtonItem = but;
    
    
    
}

- (void)shiyongjilu {
    
       
    
    
}









- (void)setupUI {
    
    
  //   self.gameArr = [[NSMutableArray alloc]init];
    
    self.navigationItem.title  = @"我的评论";
    self.navigationItem.title = [HWjianzhiduiObject keylocalization:@"我的评论"];
    
    self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.mainaTableVie];
    
    [self addrefreshControl];
    
    
    
}





- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
    //  self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

#pragma  mark --------- 下拉刷新
- (void)underRefresh {
    
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        // 结束刷新
    //        [self.mainaTableVie.mj_header endRefreshing];
    //    });
    
    
    // [SVProgressHUD show];
    
   
    
    self.p = @"1";

       __weak typeof(self) weakSelf = self;
    
      self.gameArr = [[NSMutableArray alloc]init];
    
    
    int t =   [WMUtil  getRandomNumber:101 to:998];
      
      NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
      
        NSString *userID = [HWLocallyStoredData getLoginUserID];
     //    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
         NSString *signMD5 = [WMUtil md5HexDigest:sign];
        NSString *typeStr =  @"1";
        NSDictionary *parametes = @{
                                        @"ct" :  @"app",
                                        @"ac" : @"mypinglun",
                                        @"ios" : @"1",
                                        @"uid": userID,
                                        @"t" :tStr,
                                        @"sign": signMD5,
                                        @"member_id":[HWLocallyStoredData getCode],
        
                                        };
    
//    NSDictionary *parametes = @{
//                                           @"ct" :  @"app",
//                                           @"ac" : @"mypinglun",
//                                           @"ios" : @"1",
//                                           @"uid": userID,
//                                           @"t" :tStr,
//                                           @"sign": signMD5,
//                                           @"member_id":[HWLocallyStoredData getCode],
//                                           @"id": @"3611",
//                                           @"content" : @"nishi11",
//
//
//                                           };
//
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

        if ([codeStr intValue] == 1) {
            
 
             self.gameArr =   [HWyouxipinglunModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];

            [self.mainaTableVie reloadData];
            
            
            
            
           
        } else {
            
               [self.mainaTableVie reloadData];
            
        }
        
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
}




- (void)onRefresh {
    
    
    if ([self.p  intValue] > [self.max_p intValue]) {
        
        [MBProgressHUD  showError:@"请稍后加载数据"];
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        return;
    }
    
    
     NSString *userID = [HWLocallyStoredData getLoginUserID];
     NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
     NSString *signMD5 = [WMUtil md5HexDigest:sign];
     NSString *typeStr =  @"3";
     NSDictionary *parametes = @{
                                           @"ct" :  @"app",
                                           @"ac" : @"mylb",
                                           @"ios" : @"1",
                                           @"uid": userID,
                                           @"t" :tStr,
                                           @"sign": signMD5,
                                           @"p" : self.p,
                                           @"type": typeStr,
                                           @"member_id":[HWLocallyStoredData getCode],
                                        };

    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

        if ([codeStr intValue] == 1) {
            
         //    NSArray *arr1 =  response[@"data"][@"top"];

            NSArray *arr2 =  response[@"data"];
            
            
            
//
//             NSMutableArray *mutableArr2 =    [HWxingdeLBModel mj_objectArrayWithKeyValuesArray:arr2];
//
//
//
//
//            for (int  i = 0; i <  mutableArr2 .count ; i++) {
//
//                [self.gameArr addObject:mutableArr2[i]];
//
//
//            }
//
//
//
//
//            self.p = response[@"p"];
//            self.max_p = response[@"max_p"];
//
//            [self.mainaTableVie reloadData];
//
//
            
            
           
        }
        
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
    
    
    
    
    
    
    
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {
    
    
    
       
    return  self.gameArr.count;
    
    
}

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    

    
    HWCommentsTableViewCell    *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell =   [[NSBundle mainBundle]loadNibNamed:@"HWCommentsTableViewCell" owner:nil options:nil].firstObject;
    
    }
   //  cell.tubiaoiamgeView.hidden = YES;
 //    cell.youbianshijinaLabel.constant = -56;
    cell.model = self.gameArr[indexPath.row];
    cell.delegate = self;
    
    
 
    
    
    return  cell;

}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
   //  [self calculateRowHeight:<#(NSString *)#> fontSize:<#(NSInteger)#>]
    
     HWyouxipinglunModel  *mode =   self.gameArr[indexPath.row];
    

    CGFloat gaodu =   [self calculateRowHeight:mode.content fontSize:14];
    
    

    return  82+gaodu+46+10;
    
    
    
    
    
}


- (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize

{
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
 //  NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size: 12]};
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(kScreenW-40*2, MAXFLOAT)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    
    return rect.size.height;
    
}







- (void)HWCommentsTableViewCelldeleteModel:(HWyouxipinglunModel *)model {
    
    
    
    
          NSString *userID = [HWLocallyStoredData getLoginUserID];
           NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
            NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
            NSString *signMD5 = [WMUtil md5HexDigest:sign];
          NSString *typeStr =  @"1";
            NSDictionary *parametes = @{
                                            @"ct" :  @"app",
                                            @"ac" : @"mypinglun_del",
                                            @"ios" : @"1",
                                            @"uid": userID,
                                            @"t" :tStr,
                                            @"sign": signMD5,
                                            @"id" :model.id,
                                            @"member_id":[HWLocallyStoredData getCode],
                    
                                
                                            };
        
    //    NSDictionary *parametes = @{
    //                                           @"ct" :  @"app",
    //                                           @"ac" : @"mypinglun",
    //                                           @"ios" : @"1",
    //                                           @"uid": userID,
    //                                           @"t" :tStr,
    //                                           @"sign": signMD5,
    //                                           @"member_id":[HWLocallyStoredData getCode],
    //                                           @"id": @"3611",
    //                                           @"content" : @"nishi11",
    //
    //
    //                                           };
    
    
    
    
    
    
       [MBProgressHUD show:@"正在删除"];
    
        [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
            
            // [SVProgressHUD dismiss];
       
            
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            
            [MBProgressHUD hideHUD];
            

            if ([codeStr intValue] == 1) {
                
     
             
                 [self.mainaTableVie.mj_header beginRefreshing];
                
              //  [self underRefresh];
                
                
               
            }
            
            
            
            NSLog(@"%@",response);
            
            
        } failure:^(NSString *error) {
            
            
            
            [MBProgressHUD hideHUD];
            
            
            
        }];
        
    
    
    
    
    
    
}




@end
